<?php
/*
 * kernel/object.cache.php
 *
 * This code is distributed and used under the GNU GPL v2 License,
 * please, read file LICENSE, which is situated in root directory,
 * or in "LICENSE" directory with most acceptable translation for you
 *
 * Caching engines wrapper
 *
 * Copyright (C) 2010, Taras Kudla
 *
 */

class gCache extends gKernel {
	private $supported = array("memcache","xcache");
	private $connected = array();
	private $DefaultObject = null;
	# cache expires time
	public $LIFETIME = 3600;

	# Connecting to the cache engine(s)
	function __get($engine){
		
	}

	function __set($key,$value){
	if(in_array($key,$this->supported)){
		$this->$key=$value;			
		$this->core->DEBUG("CACHE","Engine: '".$key."' linked");
		}
	}

	public function load($driver,$options ){
		if(in_array($driver,$this->connected)){
			$this->core->error("CACHE","$driver already connected");
			return false;
		} else if( !in_array($driver,$this->supported)){
			$this->core->error("CACHE","Engine '$driver' is not supported");
			return false;			
		}
		$start=microtime(1);
		$this->core->debug("Cache","Trying connect to: ".$driver);
		/*
		 * Checking file
		 */
		$file = $this->core->GetPath()."kernel/cache/".$driver.".php";
		if( !file_exists($file)){
			$this->core->error("Cache","Driver '$driver' file not found");
			return false;
		}
		require_once($file);
		$total = count($this->connected);
		/*
		 * Creating objects and links
		 */
		$driverclassname = $driver."_engine";
		$this->$driver=new $driverclassname($this->core);
		if( get_parent_class($this->$driver)==_DEFAULT_ABSTRACT_KERNEL)
			$this->$driver->init();
		$this->$driver->connect($options["server"],$options["port"]);
		if($total==0){
			$this->driver = $driver;
			$this->DefaultObject = &$this->$driver;
		}
		/*
		 * Driver connected
		 */
		$this->core->DEBUG("Cache","Driver connection:  ".$options["server"].", driver :".$driver." ... Connected");
		$this->connected[]=$driver;
		$this->core->profiler("DBO",$driver." connection time ",$start);
		return true;		
	}	

	public function connect(){
		$cache = $this->core->get("cacheconf");		
		if(isset($cache) && is_array($cache) ) {
			foreach($cache as $k=>$v){
				if(!in_array($k,$this->supported)){
					$this->core->error("Cache","Driver '$k' is unsupported.");
					return false;
				}
				else if( !is_array($v) || !isset($v["server"]) || !isset($v["port"]) ){
					$this->core->error("Cache","Driver '$k' format is unsupported.");
					return false;
				}
				else {
					$this->load($k,$v);			
				}
			}
		} else{  $this->core->error("DBO","Config file format is unsupported."); return false; }
        return true;
	}

	public function flush()	{
		$this->DefaultObject->flush();
	}
	
	public function set($key,&$data,$lifetime=null){
		$this->core->DEBUG("CACHE","Writing key '$key'");
		$this->DefaultObject->set($key,$data);
	}	
	public function get($key){
		$this->core->DEBUG("CACHE","Read key '$key'");
		return $this->DefaultObject->get($key);
	}	
	
	public function delete($key){
		return $this->DefaultObject->delete($key);
	}
	public function stats(){
		return $this->DefaultObject->stats();
	}
	
	# disconnecting all servers
	public function disconnect(){
		foreach($this->connected as $k=>$v){
			$this->$v->disconnect();
		}	
	}
	
	function __destruct(){
		$this->disconnect();
	}	
	
}